<template>
    <div class="common-box" v-if="boxIsShow">
        <div class="common-content-box">
            <div class="common-content-top-box">
                <div class="form_box">
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                        <el-form-item label="轮播图名称" prop="name">
                            <el-input v-model="ruleForm.name"></el-input>
                        </el-form-item>
                        <el-form-item label="跳转链接">
                            <el-input v-model="ruleForm.linkUrl"></el-input>
                        </el-form-item>
                        <el-form-item label="跳转链接Tips">
                          <div style="font-size:12px;color:#999">报名中心活动详情“../sign_up_desc/sign_up_desc?id=”；课程详情“../course_desc/course_desc?id=”</div>
                        </el-form-item>
                        <el-form-item label="排序" prop="level">
                            <el-input v-model="ruleForm.level"></el-input>
                        </el-form-item>
                        <el-form-item label="类型" prop="type">
                            <el-select v-model="ruleForm.type" placeholder="请选择图片类型" style="width: 100%;">
                                <el-option label="顶部轮播图" value="1"></el-option>
                                <el-option label="报名中心大图" value="2"></el-option>
                                <el-option label="首页弹框大图" value="3"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="上传图片">
                            <el-upload
                                class="upload-demo"
                                :action="UPLOAD_URL"
                                :on-success="handleSuccess"
                                multiple="false"
                                list-type="picture-card"
                                :limit="uploadNum"
                                :file-list="fileList">
                                <el-button size="small" type="primary">点击上传</el-button>
                                <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                            </el-upload>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
                            <el-button @click="resetForm('ruleForm')">重置</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
            <div class="common-content-bottom-box">
                <div class="table_box">
                    <el-table
                        :data="tableData"
                        stripe
                        style="width: 100%">
                        <el-table-column
                        prop="id"
                        label="编号"
                        width="180"
                        align="center">
                        </el-table-column>
                        <el-table-column
                        prop="name"
                        label="名称"
                        width="180"
                        align="center">
                        </el-table-column>
                        <el-table-column
                        prop="level"
                        label="排序"
                        width="180"
                        align="center">
                        </el-table-column>
                        <el-table-column
                        prop="linkUrl"
                        label="链接"
                        width="180"
                        align="center">
                        </el-table-column>
                        <el-table-column
                        label="类型"
                        align="center"
                        width="180">
                        <template slot-scope="scope">
                            {{ scope.row.type===1?'顶部轮播图':'报名中心大图' }}
                        </template>
                        </el-table-column>
                        <el-table-column
                        label="操作"
                        align="center">
                        <template slot-scope="scope">
                            <el-button @click="getDesc(scope.row.id)" type="primary" size="medium">编辑</el-button>
                            <el-button @click="del(scope.row.id)" type="danger" size="medium">删除</el-button>
                        </template>
                        </el-table-column>
                    </el-table>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import { UPLOAD_URL } from '@/utils/http.js';
export default {
  data() {
    return {
        uploadNum:1,
        UPLOAD_URL,
        boxIsShow:true,
        ruleForm: {
            id:0,
            name: '',
            level: '',
            type: '',
            linkUrl: '',
            imageUrl: ''
        },
        rules: {
          name: [
            { required: true, message: '请输入轮播图名称', trigger: ["blur,change"] }
          ],
          level: [
            { required: true, message: '请输入排序', trigger: ["blur,change"] }
          ],
          type: [
            { required: true, message: '请选择图片类型', trigger: ["blur,change"] }
          ]
        },
        fileList:[],
        tableData:[]
    };
  },
  created() {
    
  },
  mounted(){
    this.__init();
  },
  methods: {
    __init() {
      this.getDataList();
    },
    getDataList(){
      var url="manager/getBannerList";
      var params={}
      this.$post(url, params).then((res) => {
        console.log(res)
        if (res.code === 200) {
          this.tableData=res.data;
        } else {
          this.$message.error(res.msg)
        }
      });
    },
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
            if (valid) {
                var url="manager/editBanner";
                var params={
                    ...this.ruleForm
                }
                this.$post(url, params).then((res) => {
                    console.log(res)
                    if (res.code === 200) {
                        this.$message.success('操作成功')
                        this.getDataList();
                        this.resetForm('ruleForm');
                    } else {
                        this.$message.error(res.msg)
                    }
                });
            } else {
                console.log('error submit!!');
                return false;
            }
        });
    },
    resetForm(formName) {
        this.$refs[formName].resetFields();
        this.ruleForm.imageUrl='';
        this.ruleForm.linkUrl='';
        this.fileList=[];
        this.ruleForm.id=0;
    },
    handleSuccess(response, file, fileList) {
        console.log(response);
        if(response.code==200){
            this.$message.success('上传成功');
            this.ruleForm.imageUrl=response.data;
        }
    },
    del(id){
        this.$confirm('此操作将永久删除该图片, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            var url="manager/delBannerById";
            var params={
                id:id
            }
            this.$post(url, params).then((res) => {
                if (res.code === 200) {
                    this.$message.success('删除成功')
                    this.getDataList();
                } else {
                    this.$message.error(res.msg)
                }
            });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    getDesc(id){
        var url="manager/getBannerDesc";
        var params={
            id:id
        }
        this.$post(url, params).then((res) => {
            if (res.code === 200) {
                var dataDesc=res.data;
                this.ruleForm={
                    id:dataDesc.id,
                    name: dataDesc.name,
                    level: dataDesc.level,
                    type: dataDesc.type,
                    linkUrl: dataDesc.linkUrl,
                    imageUrl: dataDesc.imageUrl
                }
                this.fileList=[{name:'123',url:dataDesc.imageUrl}];
            } else {
                this.$message.error(res.msg)
            }
        });
    }
  }
}
</script>
<style scoped lang="scss">
  .common-box{
    background-color: #f9f9fb;
    height: 100%;
    position: relative;
  }
  .common-content-box{
    height: 95%;
    margin-top: 2%;
    width: 96%;
    display: inline-block;
  }
  .common-content-top-box{
    width: 100%;
    background-color: #fff;
    min-height: 200px;
    box-shadow: 1px 1px 6px 2px rgba(0,0,0,0.1);
    border-radius: 5px;
    text-align: left;
  }
  .form_box{
    margin-left: 20px;
    margin-top: 25px;
    width: 40%;
    display: inline-block;
  }
  .common-content-bottom-box{
    width: 100%;
    background-color: #fff;
    min-height: 200px;
    box-shadow: 1px 1px 6px 2px rgba(0,0,0,0.1);
    border-radius: 5px;
    margin-top: 30px;
    padding: 20px 0;
  }
  .table_box{
    width: 96%;
    margin-left: 2%;
  }
</style>